package com.xingxing.service.impl;

import com.xingxing.service.JunkMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

/**
 * @author: 小白一号
 * @date: 2022/11/18
 * @desc:
 */
@Slf4j
@Service
public class JunkMailServiceImpl implements JunkMailService {

    @Resource
    JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Override
    public boolean sendVerifyCode(String emailAccount, String veryCode) {
        try {
            MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom(from);
            message.setSubject("水牛APP");
            message.setTo(emailAccount);
            message.setText("当前正在使用邮箱注册，您的验证码为:" + veryCode + "\n如非您本人操作，请忽略此邮件！");
            this.mailSender.send(mimeMessage);
        } catch (Exception e) {
            log.error(e.toString());
            return false;
        }
        return true;
    }
}
